var http = require('https');
var fs = require('fs');
var cheerio = require('cheerio');
var request = require('request');
var xlsx = require('node-xlsx');

//初始url
// var urls = ['https://www.youyoubai.com/index.php/2020/03/18/influxdb1-7%e7%89%88%e5%ae%89%e8%a3%85%e4%b8%8e%e9%85%8d%e7%bd%aewindows%e7%8e%af%e5%a2%83%e3%80%81jmeterinfluxdbgrafana%e6%80%a7%e8%83%bd%e7%9b%91%e6%8e%a7/']
var urls = ['https://www.youyoubai.com/index.php/2020/03/18/influxdb1-7%e7%89%88%e5%ae%89%e8%a3%85%e4%b8%8e%e9%85%8d%e7%bd%aewindows%e7%8e%af%e5%a2%83%e3%80%81jmeterinfluxdbgrafana%e6%80%a7%e8%83%bd%e7%9b%91%e6%8e%a7/','https://www.youyoubai.com/index.php/2019/11/22/jsr223%e6%8e%a7%e5%88%b6%e5%99%a8/']
var url = "";

for (var j = 0; j < urls.length; j++) {
    url = urls[j];
    startRequest(url);
}

function startRequest(x) {
    //采用http模块向服务器发起一次get请求      
    http.get(x, function (res) {
        var html = ''; //用来存储请求网页的整个html内容
        var detail = [];
        res.setEncoding('utf-8'); //防止中文乱码
        //监听data事件，每次取一块数据
        res.on('data', function (chunk) {
            html += chunk;
        });
        //监听end事件，如果整个网页内容的html都获取完毕，就执行回调函数
        res.on('end', function () {
            var $ = cheerio.load(html); //采用cheerio模块解析html
            var name = $('.main > .container .post-bar .meta.text-center > h1').text().trim();
            // detail = $('html body main div div div div.container.pt-3 div.row article.article.col-lg-8 div.content').text().trim();
            savedImg($,name)
        });
    }).on('error', function (err) {
        console.log(err);
    });
}

function savedImg($, name) {
    $('html body main div article img').each(function(index, item) {
        var img_filename = index + '.jpg';
        var img_src = item.attribs.src; //获取图片的url

        if(img_src.length>100) {
            var path = './image/' + name + '---' + img_filename;
            var base64 = img_src.replace(/^data:image\/\w+;base64,/, "");//去掉图片base64码前面部分data:image/png;base64
            var dataBuffer = new Buffer(base64, 'base64'); //把base64码转成buffer对象，
            console.log('dataBuffer是否是Buffer对象：'+Buffer.isBuffer(dataBuffer));
            fs.writeFile(path,dataBuffer,function(err){//用fs写入文件
                if(err){
                    console.log(err);
                }else{
                   console.log(index);
                   console.log('写入成功！');
                }
            })
        }else {
            //采用request模块，向服务器发起一次请求，获取图片资源
            request(img_src).pipe(fs.createWriteStream('./image/' + name + '---' + img_filename));
            //通过流的方式，把图片写到本地/image目录下，并用标题和图片的标题作为图片的名称。
            console.log(index);
            console.log('写入成功！');
        }
    })
}